package Class;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.Date;

import javax.swing.JOptionPane;
import javax.swing.JTable;

public abstract class Courier_Management {
	//static Courier cr;
	//gán giá trị cho courier
	public static Courier Assign_Courier(String C_name,java.sql.Date C_Date, String s_name, double s_phone, String s_address, String r_name, double r_phone, String r_address,float amount, int status,String login_name,float weight, int s_province, int r_province){
		Courier cr =new Courier();
		cr.setCourier_name(C_name);
		cr.setDate(C_Date);
		cr.setS_name(s_name);
		cr.setS_phone(s_phone);
		cr.setS_address(s_address);
		cr.setR_name(r_name);
		cr.setR_phone(r_phone);
		cr.setR_address(r_address);
		cr.setAmount(amount);
		cr.setStatus(status);
		cr.setLogin_name(login_name);
		cr.setWeight(weight);
		cr.setS_province(s_province);
		cr.setR_province(r_province);
		return cr;
	}
	
	//create courier name follow id 
	public static String CourierName()
	{
		String courierName = "";
		try
		{
			String sql = "{call CreateCourierName(?)}";
			CallableStatement cstmt = ConnectDatabase.connection.prepareCall(sql);
			cstmt.registerOutParameter(1,java.sql.Types.VARCHAR);
			cstmt.execute();
			courierName = cstmt.getString(1);
		} catch(Exception ex) {System.out.print(ex.toString());}
		return courierName;
	}
	
	///increase id
	public static void IncreaseID()
		{
			try
			{
				CallableStatement IncID = ConnectDatabase.connection.prepareCall("{call IncreaseIDCourier()}");
				IncID.execute();	
			} catch(Exception ex) {
				System.out.println(ex.toString());
			}
		}
		
	//thêm 1 courier vào database
	public static void Add_Courier(Courier cr){
		Procedure.AddCou(cr);
		JOptionPane.showMessageDialog(null, "Add courier successfully","Message",JOptionPane.PLAIN_MESSAGE);
	}
	
	
	//delete  courier
	public static void Del_Courier(String C_Name){
		Procedure.DelCou(C_Name);
		JOptionPane.showMessageDialog(null, "Delete Successfully!");
	}
	//update 1 courier
	public static void Update_Dispatching(String C_Name, int stt){
		Procedure.Update_dis(C_Name, stt);
		JOptionPane.showMessageDialog(null, "Update Successfully!");
	}
	public static void Update_Working(Courier cr){
		Procedure.Update_Wor(cr);
	}
	//show all courier
	public static void GetAllCouier(JTable tbshow){
		Procedure.show_all(tbshow);
	}
	// get courier that has't done yet
	public static void get_cou_has_not_done( JTable tbshow){
		Procedure.get_Cou_has_not_done(tbshow);
	}
	public static Courier Info_Courier(String C_Name){
		return Procedure.Info_Cou(C_Name);
	}
	//
	public static void showtable(JTable tbshow, String S_name, String R_name, String Login_name,java.sql.Date date){
		Procedure.show_info(tbshow, S_name, R_name,Login_name,date);
	}
	// kiem tra database xem da ton tại procedure do hay chưa
	public static int Check_courier(String courier_Name){
		return Procedure.check_courier(courier_Name);
	}
	public static void find(JTable tbshow, String C_Name, String S_Name, String R_Name, String LoginName){
		if(!C_Name.equals("")){ 
			if(Procedure.check_courier(C_Name)==1){	
				Procedure.show_info_byCouName( tbshow, C_Name);
			}else{
				Procedure.show_info2(tbshow, S_Name, R_Name, LoginName);
			}
			
		} else{
			Procedure.show_info2(tbshow, S_Name, R_Name, LoginName);
		};

	}
	//find courier
	public static void find(JTable tbshow, String C_Name, String S_Name, String R_Name, String LoginName, java.sql.Date date){
		if(!C_Name.equals("")){ 
			if(Procedure.check_courier(C_Name)==1){	
				Procedure.show_info_byCouName( tbshow, C_Name);
			}else{
				Procedure.show_info(tbshow, S_Name, R_Name, LoginName,date);
			}
			
		} else{
			Procedure.show_info(tbshow, S_Name, R_Name, LoginName,date);
		};

	}
	//check for whether has the road from Sender province to receiver province 
	public static float check_distance(int S_province, int R_province){
		float temp=0;
		if(Procedure.getdistance(S_province, R_province)!=0)
			temp =Procedure.getdistance(S_province, R_province);
		else if(Procedure.getdistance( R_province,S_province)!=0)
			temp =Procedure.getdistance( R_province,S_province);
		return temp;
	}
	//add a distance
	public static int add_distance(int S_province, int R_province, float distance){
		float temp=check_distance(S_province, R_province);
		if(temp==0){
			Procedure.add_distance(S_province, R_province, distance);
		}
		else{
			JOptionPane.showMessageDialog(null, "It already has a road between two city");
		}
		
		
		return 0;
	}
	//Add city 
	public static void add_city(String location){
		Procedure.add_city(location);
	}
	// calculate the courier bases on weight and distance
	public static float calculate(int S_province, int R_province, float weight){
		float amount=0;
		float distance=0;
		float temp=0;
		int dis_Index =0;
		float price=0;
		float addition=0;
		try {
			temp =(float) Math.ceil(weight);
			distance = check_distance(S_province, R_province);
			dis_Index = Procedure.GetDistanceIndex(distance,S_province, R_province);
			
			
				CallableStatement getcost = ConnectDatabase.connection.prepareCall("{Call GETCOST(?,?,?)}");
				getcost.setInt(1, dis_Index);
				getcost.registerOutParameter(2, java.sql.Types.FLOAT);
				getcost.registerOutParameter(3,  java.sql.Types.FLOAT);
				getcost.execute();
				price = getcost.getFloat(2);
				addition = getcost.getFloat(3);
				//==============================
			if(dis_Index==6){
				amount=0;
			}
			else
			if(dis_Index==1){
				if(weight==0){
					amount = 0;
				}else {
					if(weight<=2 && weight>0) amount =price;
					else{
						amount = price +(temp-2)*addition;
					}
				}			
			}
			else{
				if(weight ==0){
					amount=0;
				}
				else{
					if(weight<=2 && weight>0){
						if(dis_Index==2){
							amount=price ;
						}else if(dis_Index==3){
							amount=price;
						}else if(dis_Index==4){
							amount=price;
						}else if(dis_Index==5){
							amount=price;
						}
					}
				else{
						if(dis_Index==2){
							amount=price + (temp-2)*addition;
						}else if(dis_Index==3){
							amount=price + (temp-2)*addition;
						}else if(dis_Index==4){
							amount=price + (temp-2)*addition;
						}else if(dis_Index==5){
							amount=price + (temp-2)*addition;
						}
					}
				}
			}
			//==============================================

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return amount;
	}
	
	
}
